//
//  NewFeatureView.swift
//  FWSinaWeibo
//
//  Created by 范伟 on 16/2/26.
//  Copyright © 2016年 FanWei. All rights reserved.
//

import UIKit
import SnapKit

private let FWNewFeatureViewImageCount = 4
/// 自定义新特性视图
class NewFeatureView: UIView {

    //MARK: 分页控件
    private lazy var pageControl: UIPageControl = { () -> UIPageControl in
        let pageControl = UIPageControl()
        pageControl.hidesForSinglePage = true
        pageControl.numberOfPages = FWNewFeatureViewImageCount
        pageControl.currentPage = 0
        pageControl.currentPageIndicatorTintColor = UIColor.lightGrayColor()
        pageControl.pageIndicatorTintColor = FWAppAppearence
//        pageControl.userInteractionEnabled = false
        return pageControl
    }()
    
    override init(frame: CGRect) {
        super.init(frame: UIScreen.mainScreen().bounds)
//        backgroundColor = UIColor.lightGrayColor()
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    deinit {
        NSLog("\(classForCoder) \(__FUNCTION__)")
    }
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        removeFromSuperview()
    }
    private func setupUI() {
        let scrollView = UIScrollView(frame: bounds)
        addSubview(scrollView)
        
        for i in 0..<4 {
            let imageView = UIImageView(fw_imageName: "new_feature_\(i + 1)")
            scrollView.addSubview(imageView)
//            Returns a rectangle with an origin that is offset from that of the source rectangle.
            imageView.frame = CGRectOffset(bounds, CGFloat(i) * bounds.width, 0)
        }
        //设置contentSize
        scrollView.contentSize = CGSize(width: CGFloat(FWNewFeatureViewImageCount) * bounds.width, height: 0)
        
        scrollView.bounces = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.pagingEnabled = true
        
        addSubview(pageControl)
        
        pageControl.snp_makeConstraints { make in
            make.centerX.equalTo(self)
            make.bottom.equalTo(self).offset(-80)
        }
    }
}
